package com.efast.dmom.mes.datahandlers.entity;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.dmom.mes.bean.Plant;
import com.efast.dmom.mes.bean.ResultBean;
import com.efast.dmom.mes.dao.entity.PlantDao;
import com.efast.dmom.mes.datahandlers.base.PlantModelDataHandler;
import com.efast.dmom.mes.entity.PlantEntity;
import com.efast.dmom.mes.util.BaseEntityDataUtil;
import com.efast.dmom.mes.util.TimeUtil;
@Component
public class PlantDataHandler extends PlantModelDataHandler<Plant>{

	@Autowired
	private PlantDao plantDao;

	@Override
	public ResultBean save(Plant obj) {
		ResultBean resultBean = new ResultBean(true);
		if(obj==null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("paramater is null");
			return resultBean;
		}
		PlantEntity innerData = obj.getInnerData();
		if(innerData==null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("paramater innerdata is null");
			return resultBean;
		}
		BaseEntityDataUtil.setBaseEntityData(innerData);
		int save = plantDao.save(innerData);
		if(save>0) {
			resultBean.setSuccess(true);
		}else {
			resultBean.setSuccess(false);
			resultBean.setMessage("insert fail");
		}
		resultBean.setObject(obj);
		return resultBean;
	}
	@Override
	public ResultBean update(Plant obj) {
		ResultBean resultBean = new ResultBean();
		if (obj.getInnerData() != null) {

			obj.getInnerData().setLastModifiedTime(TimeUtil.getLocalDateTime());
			obj.getInnerData().setLastModifier(SpringWebUtils.getRequestUser());
			obj.getInnerData().setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
//			obj.getInnerData().setLastModifiedPid(DataHandlerUtils.getDmomPid());

			plantDao.update(obj.getInnerData());
		}
		return resultBean;

	}
	@Override
	public ResultBean delete(Plant obj) {
		ResultBean resultBean = new ResultBean();
		if(obj.getInnerData() != null){

			obj.getInnerData().setLastModifiedTime(TimeUtil.getLocalDateTime());
			obj.getInnerData().setLastModifier(SpringWebUtils.getRequestUser());
			obj.getInnerData().setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
			// obj.getInnerData().setLastModifiedPid(DataHandlerUtils.getDmomPid());
			
			plantDao.delete(obj.getInnerData());
		}
		return resultBean;
	}

	public Plant getByCode(String plantCode) {
		Plant plant = new Plant();
		plant.setInnerData(plantDao.getByCode(plantCode));
		return plant;
	}

	public Plant getById(long id) {
		Plant plant = new Plant();
		plant.setInnerData(plantDao.getById(id));
		return plant;
	}

	public Plant getByName(String name) {
		Plant plant = new Plant();
		plant.setInnerData(plantDao.getByName(name));
		return plant;
	}
	
	public List<Plant> getAllPlant(){
		List<PlantEntity> allPlant = plantDao.getAllPlant();
		List<Plant> plants = new ArrayList<Plant>();
		if(allPlant != null && allPlant.size() > 0) {
		for(PlantEntity plantEntity:allPlant) {
			Plant plant = new Plant();
			plant.setInnerData(plantEntity);
			plants.add(plant);
		}
		return plants;
	}
		return null;
	}
	
	
	public List<Plant> getPlantByName(String name){
		List<PlantEntity> allPlant = plantDao.getPlantByName(name);
		List<Plant> plants = new ArrayList<Plant>();
		if(allPlant != null && allPlant.size() > 0) {
		for(PlantEntity plantEntity:allPlant) {
			Plant plant = new Plant();
			plant.setInnerData(plantEntity);
			plants.add(plant);
		}
		return plants;
	}
		return null;
	}
	public List<Map<String,String>> queryPlantCodeIdNameList(){
		return plantDao.queryPlantCodeIdNameList();
	}

	@Override
	public ResultBean saveBatch(List<Plant> obj) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public ResultBean updateBatch(List<Plant> obj) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public ResultBean deleteBatch(List<Plant> obj) {
		// TODO Auto-generated method stub
		return null;
	}

}
